package com.ghpt.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ghpt.dto.DianDailyAvgDTO;
import com.ghpt.dto.DianXQUpdateDTO;
import com.ghpt.pojo.DianXQ;
import com.ghpt.pojo.SolarRadiation;
import com.ghpt.service.DianXQService;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/dianxq")
public class DianXQController {
    private final DianXQService dianXQService;

    public DianXQController(DianXQService dianXQService) {
        this.dianXQService = dianXQService;
    }
    @GetMapping("/{id}")
    public DianXQ getDianXQById(@PathVariable Long id) {
        System.out.println("结果是"+dianXQService.findDianXQById(id));
        return dianXQService.findDianXQById(id);
    }
    @PutMapping
    public int updateDianXQ(@RequestBody DianXQ dianXQ) {
        return dianXQService.updateById(dianXQ);
    }
    @GetMapping("/all")
    public List<DianXQ> getAllDianXQ() {
        List<DianXQ> list = new ArrayList<>();
        for (long i = 1; i <= 96; i++) {
            list.add(dianXQService.findDianXQById(i));
        }
        return list;
    }
    @GetMapping("/page")
    public IPage<DianXQ> getDianXQByPage(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "24") int size) {
        Page<DianXQ> pageParam = new Page<>(page, size);
        // 只查询id从97开始的数据
        return dianXQService.findDianXQByPage(pageParam);
    }

    /**
     * 获取所有天数的平均太阳辐射数据
     * @return 365天的平均值列表
     */
    @GetMapping("/daily-average")
    public List<DianDailyAvgDTO> getDailyAverageDianXQ(@RequestParam Integer years) {
        return dianXQService.calculateDailyAverage(years);
    }
    @PutMapping("single/{id}")
    public int updateValueById(@PathVariable Long id, @RequestBody DianXQUpdateDTO dto) {
        return dianXQService.updateValue(id, dto.getValue());
    }

    @PostMapping("/upload/{startId}")
    public ResponseEntity<?> upload(@RequestParam("file") MultipartFile file,
                                    @PathVariable("startId") Long startId) {
        if (file.isEmpty()) {
            return ResponseEntity.badRequest().body(Map.of("message", "文件为空"));
        }

        List<DianXQ> dataList = new ArrayList<>();

        try (Reader reader = new InputStreamReader(file.getInputStream());
             CSVParser csvParser = CSVFormat.DEFAULT.parse(reader)) {

            int count = 0;
            for (CSVRecord record : csvParser) {
                DianXQ entity = new DianXQ();
                entity.setId(startId + count); // 自动设置 ID 范围
                entity.setValue(Double.parseDouble(record.get(0).trim()));
                dataList.add(entity);
                count++;
            }

            if (count != 24) {
                return ResponseEntity.badRequest()
                        .body(Map.of("message", "数据条数不为24条！"));
            }

            dianXQService.replaceRows(startId, startId + 23, dataList);
            return ResponseEntity.ok(Map.of("message", "数据导入成功！"));

        } catch (IOException | NumberFormatException e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Map.of("message", "文件解析失败或格式错误"));
        }
    }
    @PostMapping("/upload/8760/{startId}/{N}")
    public ResponseEntity<?> upload8760(@RequestParam("file") MultipartFile file,
                                        @PathVariable("startId") Long startId,@PathVariable("N") Long N) {
        if (file.isEmpty()) {
            return ResponseEntity.badRequest().body(Map.of("message", "文件为空"));
        }

        List<DianXQ> dataList = new ArrayList<>();

        try (Reader reader = new InputStreamReader(file.getInputStream());
             CSVParser csvParser = CSVFormat.DEFAULT.parse(reader)) {

            int count = 0;
            for (CSVRecord record : csvParser) {
                DianXQ entity = new DianXQ();
                entity.setId(startId + count); // 自动设置 ID 范围
                entity.setValue(Double.parseDouble(record.get(0).trim()));
                dataList.add(entity);
                count++;
            }

            if (count != (8760*N)) {
                return ResponseEntity.badRequest()
                        .body(Map.of("message", "数据条数不为"+8760*N+"条！"));
            }

            dianXQService.replaceRows(startId, startId + (8760*N-1), dataList);
            return ResponseEntity.ok(Map.of("message", "数据导入成功！"));

        } catch (IOException | NumberFormatException e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Map.of("message", "文件解析失败或格式错误"));
        }
    }
<<<<<<< HEAD
}
=======
}

>>>>>>> 26b5c7b9dacdd13c6dcdf4324dd86fe1b533b2c9
